package com.whfc.cncit.helmet.codec;

import com.whfc.cncit.constant.CncitConst;
import com.whfc.cncit.entity.CncitUpMsgH;
import com.whfc.cncit.entity.CncitUpMsgS;
import com.whfc.cncit.helmet.util.CncitMsgUtil;
import com.whfc.common.util.JSONUtil;
import com.whfc.iot.util.ByteUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @Description:
 * @author: xugcheng
 * @version: 1.0
 * @date: 2019/10/25 15:13
 */
public class CncitUpMsgCodecS implements CncitMsgCodec<CncitUpMsgS> {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    public CncitUpMsgS decode(String data) {
        CncitUpMsgS msg = null;
        //$6F,S,131910027100001,N,3028.53985,E,11424.89409,0.037,86.90,VER2,11,0.97,251019-070905,31,0,8,396,70,038,070,076,#
        String[] dataArr = data.split(CncitConst.DELIMITER);
        if (dataArr != null && dataArr.length >= 18) {
            String cmd = dataArr[1];
            String deviceCode = dataArr[2];
            String latFlag = dataArr[3];
            String latStr = dataArr[4];
            String lngFlag = dataArr[5];
            String lngStr = dataArr[6];
            String sppeedStr = dataArr[7];
            String rotationStr = dataArr[8];
            String version = dataArr[9];
            String geoNumStr = dataArr[10];
            String levelFactor = dataArr[11];
            String timeStr = dataArr[12];
            String rssiStr = dataArr[13];
            String alarmValueStr = dataArr[14];
            String cardStateValueStr = dataArr[15];
            String batterVoltStr = dataArr[16];
            String batteryPowerStr = dataArr[17];
//            String accelerationX = dataArr[18];
//            String accelerationY = dataArr[19];
//            String accelerationZ = dataArr[20];

            //解析定位状态值
            int cardStateValue = ByteUtil.fromHexString(cardStateValueStr);
            int cBit0 = CncitMsgUtil.getBitValue(cardStateValue, 0);
            int cBit1 = CncitMsgUtil.getBitValue(cardStateValue, 1);
            int cBit2 = CncitMsgUtil.getBitValue(cardStateValue, 2);
            int cBit3 = CncitMsgUtil.getBitValue(cardStateValue, 3);
            int posType = cBit0;
            int posState = cBit1 == 0 ? 1 : 0;
            int posMode = (cBit3 == 0 && cBit2 == 0) ? 0 : (cBit3 == 0 && cBit3 == 1) ? 1 : 2;

            //解析报警值
            int alarmValue = ByteUtil.fromHexString(alarmValueStr);

            msg = CncitConst.UP_CMD_S.equals(cmd) ? new CncitUpMsgS() : new CncitUpMsgH();
            msg.setDeviceCode(deviceCode);
            msg.setLatFlag(latFlag);
            msg.setLatWgs84(CncitMsgUtil.parseLat(latStr));
            msg.setLngFlag(lngFlag);
            msg.setLngWgs84(CncitMsgUtil.parseLng(lngStr));
            msg.setSpeed(CncitMsgUtil.parseDouble(sppeedStr));
            msg.setRotation(CncitMsgUtil.parseDouble(rotationStr));
            msg.setVersion(version);
            msg.setGeoNum(CncitMsgUtil.parseInt(geoNumStr));
            msg.setLevelFactor(CncitMsgUtil.parseDouble(levelFactor));
            msg.setTime(CncitMsgUtil.parseTime(timeStr));
            msg.setRssi(CncitMsgUtil.parseInt(rssiStr));
            msg.setAlarmValue(alarmValue);
            msg.setCardStateValue(cardStateValue);
            msg.setPosType(posType);
            msg.setPosState(posState);
            msg.setPosMode(posMode);
            msg.setBatterVolt(CncitMsgUtil.parseDouble(batterVoltStr) / 100D);
            msg.setBatteryPower(CncitMsgUtil.parseInt(batteryPowerStr));
//            msg.setAccelerationX(CncitMsgUtil.parseDouble(accelerationX) / 100D);
//            msg.setAccelerationY(CncitMsgUtil.parseDouble(accelerationY) / 100D);
//            msg.setAccelerationZ(CncitMsgUtil.parseDouble(accelerationZ) / 100D);
        }
        return msg;
    }

    @Override
    public String encode(CncitUpMsgS msg) {
        return null;
    }

    public static void main(String[] args) {
        //String data = "$6F,S,131910027100001,N,3028.54272,E,11424.93102,0.094,42.50,VER2,10,1.58,251019-075146,31,0,8,394,68,038,072,076,";
        String data = "$59,S,1161002,N,0000.0000,E,00000.0000,00000,061.0,VER2,00,99.0,060611-000000,27,0,A,379,61,";
        CncitUpMsgCodecS codec = new CncitUpMsgCodecS();
        CncitUpMsgS msg = codec.decode(data);
        System.out.println(JSONUtil.toPrettyString(msg));
    }
}
